
package com.jf.cloud.multishop.service.impl;

import com.jf.cloud.api.multishop.vo.ShopRefundAddrVO;
import com.jf.cloud.common.cache.constant.CacheNames;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.multishop.dto.ShopRefundAddrDTO;
import com.jf.cloud.multishop.mapper.ShopRefundAddrMapper;
import com.jf.cloud.multishop.model.ShopRefundAddr;
import com.jf.cloud.multishop.service.ShopRefundAddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 商家退货地址
 *
 * @author zz
 * @date 2021-03-09 15:03:18
 */
@Service
public class ShopRefundAddrServiceImpl implements ShopRefundAddrService {

    @Autowired
    private ShopRefundAddrMapper shopRefundAddrMapper;

    @Override
    public PageVO<ShopRefundAddrVO> page(PageDTO pageDTO, ShopRefundAddrDTO shopRefundAddrDTO) {
        return PageUtil.doPage(pageDTO, () -> shopRefundAddrMapper.list(shopRefundAddrDTO));
    }

    @Override
    public ShopRefundAddrVO getByShopRefundAddrId(Long shopRefundAddrId, Long shopId) {
        return shopRefundAddrMapper.getByShopRefundAddrId(shopRefundAddrId, shopId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(ShopRefundAddr shopRefundAddr) {
        if (Objects.equals(shopRefundAddr.getIsDefault(), 1)) {
            shopRefundAddrMapper.cancelDefaultAddr(shopRefundAddr.getShopId());
        }
        shopRefundAddrMapper.save(shopRefundAddr);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ShopRefundAddr shopRefundAddr) {
        ShopRefundAddrVO dbShopRefund = getByShopRefundAddrId(shopRefundAddr.getShopRefundAddrId(), shopRefundAddr.getShopId());
        if (Objects.isNull(dbShopRefund)) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        } else if (!Objects.equals(shopRefundAddr.getShopId(), dbShopRefund.getShopId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        if (Objects.equals(shopRefundAddr.getIsDefault(), 1) && Objects.equals(dbShopRefund.getIsDefault(), 0)) {
            shopRefundAddrMapper.cancelDefaultAddr(shopRefundAddr.getShopId());
        }
        shopRefundAddrMapper.update(shopRefundAddr);
    }

    @Override
    public void deleteById(Long shopRefundAddrId, Long shopId) {
        ShopRefundAddrVO dbShopRefund = getByShopRefundAddrId(shopRefundAddrId, shopId);
        if (Objects.isNull(dbShopRefund)) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        } else if (!Objects.equals(dbShopRefund.getShopId(), shopId)) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        shopRefundAddrMapper.deleteById(shopRefundAddrId);
    }

    @Override
    @Cacheable(cacheNames = CacheNames.SHOP_REFUND_ADDR, key = "#shopId", sync = true)
    public List<ShopRefundAddrVO> listByShopId(Long shopId) {
        return shopRefundAddrMapper.listByShopId(shopId);
    }

    @Override
    @CacheEvict(cacheNames = CacheNames.SHOP_REFUND_ADDR, key = "#shopId")
    public void removeCacheByShopId(Long shopId) {
    }
}
